package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanGoingOutData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserZhenHuanGoingOutDaoImpl extends TempDataObjectDao<UserZhenHuanGoingOutData> {

    public static String getUserZhenHuanGoingOutDataSql = "select * from `t_u_zhenhuan_going_out` where `activityId`=? and `userId`=?";
    public Map<Integer, UserZhenHuanGoingOutData> getUserZhenHuanGoingOutData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserZhenHuanGoingOutData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanGoingOutDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserZhenHuanGoingOutData data = parse(rs);
                dataMap.put(data.getPlaceId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserZhenHuanGoingOutDataSql = "update `t_u_zhenhuan_going_out` set progress=?,amount=? where activityId=? and userId=? and placeId=?";
    @Override
    protected boolean updateData(UserZhenHuanGoingOutData userZhenHuanGoingOutData) {
        userZhenHuanGoingOutData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanGoingOutData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getPlaceId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanGoingOutDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanGoingOutDataSql = "insert into `t_u_zhenhuan_going_out`(activityId,userId,placeId,progress,amount) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanGoingOutData userZhenHuanGoingOutData) {
        userZhenHuanGoingOutData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanGoingOutData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanGoingOutData.getAmount()));
        if (getDb().executeNoneQuery(insertUserZhenHuanGoingOutDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanGoingOutData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanGoingOutData userZhenHuanGoingOutData = new UserZhenHuanGoingOutData();
        userZhenHuanGoingOutData.setActivityId(rs.getInt("activityId"));
        userZhenHuanGoingOutData.setUserId(rs.getLong("userId"));
        userZhenHuanGoingOutData.setPlaceId(rs.getInt("placeId"));
        userZhenHuanGoingOutData.setProgress(rs.getInt("progress"));
        userZhenHuanGoingOutData.setAmount(rs.getInt("amount"));

        return userZhenHuanGoingOutData;
    }
}
